<?php
  require_once('functions.php');

class DataBase extends Common
{
  /*
   * MYSQL Database connection string
   * */
  //$hostName = 'localhost';
  private $hostName = '127.0.0.1';
  private $username = 'root';
  private $password = '';
  private $dbname = 'mb126';

  function __construct()
  {
    $link = mysql_connect($this->hostName,$this->username,$this->password )or
                   die(mysql_error());

    /*
     * Database selection
     */
    mysql_select_db($this->dbname,$link) or die(mysql_error());
  }

  /****
   * Used to insert row in DB
   * @param $tableName STRING "Name of db table"
   * @param $data ARRAY "Column name aand value pair of array"
   * @return INT "Inserted Id"
   */
  function insert($tableName='',$data=array())
  {
    $query = "INSERT INTO `$tableName` SET";
    $subQuery = '';
    foreach ($data as $columnName => $colValue) {
      $subQuery .= " `$columnName`='$colValue',";
    }
    $subQuery = rtrim($subQuery,', ');
    $query .= $subQuery;
    $this->pr($query);
    mysql_query($query) or die(mysql_error());
    return mysql_insert_id();
  }//end insert


  function fetchAll($table = '',$where='',$order='',$limit='',$offset=0)
  {
    if($offset != 0){
      $limit = "LIMIT $limit,$offset";
    }

    if($order != '') {
      $order = "ORDER BY $order";
    }

    if($where != '') {
      $where = "WHERE  $where";
    }

    $query = "SELECT * FROM $table  $where $order $limit";

    $result = mysql_query($query) or die(mysql_error());
    return $result;
  }//fetchAll()


  function fetchById($table = '',$id=0)
  {

    if($id != 0) {
      $where = "WHERE `id`=$id";
    }

    $query = "SELECT * FROM $table  $where";
    $this->pr($query);
    $result = mysql_query($query) or die(mysql_error());
    return mysql_fetch_assoc($result);
  }//fetchAll()





  function update($tableName='',$data=array(),$where=''){
    $query = "UPDATE `$tableName` SET";
    $subQuery = '';
    foreach ($data as $columnName => $colValue) {
      $subQuery .= " `$columnName`='$colValue',";
    }
    $subQuery = rtrim($subQuery,', ');

    $where = " WHERE $where";
    $query .= $subQuery.$where;
    $this->pr($query);
    mysql_query($query) or die(mysql_error());
    return mysql_affected_rows();
  }


  function updateById($tableName='',$data=array(),$id=0){

    if(empty($tableName) || empty($data)) {
      return false;
    }

    $subQuery = '';
    foreach ($data as $columnName => $colValue) {
      $subQuery .= " `$columnName`='$colValue',";
    }
    $subQuery = rtrim($subQuery,', ');
    $query = "UPDATE `$tableName` SET $subQuery WHERE `id`=$id";
    $this->pr($query);
    mysql_query($query) or die(mysql_error());
    return mysql_affected_rows();
  }

  /*
   * USed to get total number of records in any table
   * @param $tableName STRING "Name of table from which we want to fetch"
   * @return INT total records
   *
   */
  function countAll($tableName = '',$where='')
  {
    if($where != '') {
      $where = "WHERE  $where";
    }


    $query = "SELECT COUNT(*) as `total` FROM `$tableName` $where";
    $this->pr($query);
    $result = mysql_query($query)or die(mysql_error());
    $ans = mysql_fetch_assoc($result);
    return $ans['total'];

  }


  /*
   * USed to DELETE record in any table By ID
   * @param $tableName STRING "Name of table
   * in which we want to delete"
   * @param $id INT
   * @return INT "Number of deleted rows"
   */
  function deleteById($tableName = '',$id=0)
  {

    if(empty($tableName) || $id==0) {
      return false;
    }

    $query = "DELETE FROM `$tableName` WHERE `id`=$id";
    $this->pr($query);
    mysql_query($query) or die(mysql_error());
    return mysql_affected_rows();
  }



  function getCategories()
  {
    $cat = array('1'=>'Sony',
                 '2'=>'Apple',
                 '3'=>'Samsung',
                 '4'=>'Toshiba',
                 '5'=>'Asus',
                 '6'=>'LG');
    return $cat;
  }

  function getCategoriesDropdown($name='')
  {
    if($name=='')return false;

    $allCat = getCategories();

    $select = "<select name=\"$name\">";
    $select .= '<option>-SELECT-</option>';

    foreach( $allCat as $catId=>$catName) {
      $select .= "<option value=\"$catId\">$catName</option>";
    }
    $select .= '</select>';
    return $select;
  }

}
